﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Imaginecup.util.vertex2D
{
    class Vertex2DUtil
    {
        public static float fangle(Vector2 vect)
        {
           if (vect.X == 0.0 && vect.Y == 0.0) {
               return 0;
           }

           if (vect.X == 0.0)
           {
               return vect.Y > 0 ? MathHelper.Pi / 2 : -MathHelper.Pi / 2;
           }

           if (vect.Y == 0.0 && vect.X < 0)
           {
               return -MathHelper.Pi;
           }

           float angle = (float)Math.Atan(vect.Y / vect.X);

           return vect.X < 0 ? angle + MathHelper.Pi : angle;
        }
        //public static float lagrange1(Vector2 p1, Vector2 p2, float x)
        //{
        //    return 0;
        //}

        public static Vector2 ccpRotateByAngle(Vector2 v, Vector2 pivot, float angle)
        {
            Vector2 r = Vector2.Subtract(v, pivot);
            float t = r.X;
            double cosa = Math.Cos(angle);
            double sina = Math.Sin(angle);

            r.X = (float)(t * cosa - r.Y * sina);
            r.Y = (float)(t * sina + r.Y * cosa);
            r = Vector2.Add(r, pivot);
            return r;
        }

        public static void f1(Vector2 p1, Vector2 p2, float d, Vertex2DBuffer outPutArray , int outIndex1 ,  int outIndex2)
        {
            float l = Vector2.Distance(p1, p2);
            float angle = fangle(Vector2.Subtract(p2, p1));


            outPutArray.SetVertexPos2D(outIndex1, ccpRotateByAngle(new Vector2(p1.X + l, p1.Y + d), p1, angle));
            outPutArray.SetVertexPos2D(outIndex2, ccpRotateByAngle(new Vector2(p1.X + l, p1.Y - d), p1, angle));
            //    *o1 = ccpRotateByAngle(ccp(p1.x + l,p1.y + d), p1, angle);
            //    *o2 = ccpRotateByAngle(ccp(p1.x + l,p1.y - d), p1, angle);
        }
    }
}


//inline void f1(CGPoint p1, CGPoint p2, float d, CGPoint *o1, CGPoint *o2){
//    float l = ccpDistance(p1, p2);
//    float angle = fangle(ccpSub(p2, p1));
//    *o1 = ccpRotateByAngle(ccp(p1.x + l,p1.y + d), p1, angle);
//    *o2 = ccpRotateByAngle(ccp(p1.x + l,p1.y - d), p1, angle);
//}

//inline float lagrange1(CGPoint p1, CGPoint p2, float x){
//    return (x-p1.x)/(p2.x - p1.x)*p2.y + (x-p2.x)/(p1.x - p2.x)*p1.y ;
//}

//inline void CGPointSet(CGPoint *v, float x, float y){
//    v->x = x;
//    v->y = y;
//}